package com.yylending.las.lending.quota.credit;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.YJKD;
import com.yylending.las.common.AESEntity;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by laixj on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class CreditPresenter extends Presenter<CreditModel, CreditPresenter.View> {

	private final static String TAG = "CreditPresenter";
	private static final String RET_CODE_CREDIT_HAS_LIMITED = "100019"; // 征信查询达到上限

	public static class Generator implements PresenterFactory<CreditPresenter> {
		@Override
		public CreditPresenter create() {
			return new CreditPresenter();
		}
	}

	public interface View extends IView {
		void getVerifyInfoSuccess(CreditModel.VerifyInfo verifyInfo);

		void creditHasLimited();

		void getVerifyInfoFail(String msg);

		void saveVerifyCodeSuccess();

		void saveVerifyCodeFail(String msg);

		void saveVerifyCodeRetCode(String retCode);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new CreditModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getVerifyInfoResult(CreditModel.VerifyInfoResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result) && null != result.getData()) {
				mView.getVerifyInfoSuccess(result.getData());
			} else if (TextUtils.equals(RET_CODE_CREDIT_HAS_LIMITED, result.getRetCode())){
				mView.creditHasLimited();
			} else {
				String msg = result.getRetMsg();
				mView.getVerifyInfoFail(TextUtils.isEmpty(msg) ? "获取征信报文失败" : result.getRetMsg());
			}
		}
	}

	/**
	 * 获取征信报文
	 */
	public void getVerifyInfo(String sourceType, String metUrl) {
		execute(new AESEntity(new CreditModel.GetVerifyInfo(sourceType, metUrl)), CreditModel.METHOD_GET_VERIFY_INFO, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void saveCodeResult(CreditModel.SaveVerifyCodeResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				if (result.getData() != null && !TextUtils.isEmpty(result.getData().userName)) {
					Global.USER_NAME = result.getData().userName;
					YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_USER_NAME, result.getData().userName);
				}
				mView.saveVerifyCodeSuccess();
			} else if (TextUtils.equals(RET_CODE_CREDIT_HAS_LIMITED, result.getRetCode())){
				mView.creditHasLimited();
			} else {
				String msg = result.getRetMsg();
				mView.saveVerifyCodeFail(TextUtils.isEmpty(msg) ? "保存征信验证码失败" : result.getRetMsg());
			}
			mView.saveVerifyCodeRetCode(result.getRetCode());
		}
	}

	/**
	 * 保存征信验证码
	 */
	public void saveVerifyCode(String verifyCode, String longitude, String latitude) {
		execute(new AESEntity(new CreditModel.SaveVerifyCode(verifyCode, longitude, latitude)), CreditModel.METHOD_SAVE_AUTH_CODE, false, null, mRequestPage);
	}
}
